package com.zhou.test.utils;

import org.apache.log4j.Logger;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Set;

public class RedisUtils {
    protected static Logger logger = Logger.getLogger(RedisUtils.class);

    private static JedisPool jedisPool;

    public void setJedisPool(JedisPool jedisPool) {
        this.jedisPool = jedisPool;
    }

    /**
     * 获取Jedis实例
     *
     * @return
     */
    public synchronized static Jedis getJedis() {
        try {
            if (jedisPool != null) {
                Jedis resource = jedisPool.getResource();
                return resource;
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    private static String getKey(String key) {
        return "config_" + key;
    }

    /**
     * 设置 String
     *
     * @param key
     * @param value
     */
    public static void setString(String key, String value) {
        try {
            key = getKey(key);
            value = StringUtil.isEmpty(value) ? "" : value;
            getJedis().set(key, value);
        } catch (Exception e) {
            logger.error("Set key error : " + e);
        }
    }

    /**
     * 设置 过期时间
     *
     * @param key
     * @param seconds 以秒为单位
     * @param value
     */
    public static void setString(String key, String value, int seconds) {
        try {
            key = getKey(key);
            getJedis().setex(key, seconds, value);
        } catch (Exception e) {
            logger.error("Set keyex error : " + e);
        }
    }

    /**
     * 设置 过期时间
     *
     * @param key
     * @param seconds 以秒为单位
     * @param value
     */
    public static void setByte(byte[] key, byte[] value, int seconds) {
        try {
            //key = getKey(key);
            getJedis().setex(key, seconds, value);
        } catch (Exception e) {
            logger.error("Set keyex error : " + e);
        }
    }

    /**
     * 获取String值
     *
     * @param key
     * @return value
     */
    public static String getString(String key) {
        key = getKey(key);
        if (getJedis() == null || !getJedis().exists(key)) {
            return null;
        }
        return getJedis().get(key);
    }

    /**
     * 获取String值
     *
     * @param key
     * @return value
     */
    public static byte[] getByte(byte[] key) {
        //key = getKey(key);
        if (getJedis() == null || !getJedis().exists(key)) {
            return null;
        }
        return getJedis().get(key);
    }

    /**
     * keys
     *
     * @param pattern
     * @return
     */
    public Set<byte[]> keys(String pattern) {
        pattern = pattern + "*";
        Set<byte[]> keys = null;
        keys = getJedis().keys(pattern.getBytes());
        return keys;
    }

    /**
     * del
     *
     * @param key
     */
    public void del(String key) {
        getJedis().del(key);
    }

    /**
     * del
     *
     * @param key
     */
    public void del(byte[] key) {
        getJedis().del(key);
    }

    /**
     * flush
     */
    public void delByKeyPrefix(String keyPrefix) {
        Set<byte[]> keys = keys(keyPrefix);
        for (byte[] key : keys) {
            getJedis().del(key);
        }
    }

    /**
     * size
     */
    public Long dbSize() {
        return getJedis().dbSize();
    }
}
